package GUI;

/**
 * AT AUTOINDENT ITT MEGÖLI A KÓDOT (MEG A KÓDOLÓT IS)
 * GraphicalView
 * @author bg
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Logger;
import javax.swing.*;

public class GraphicalView extends MouseAdapter {
    
    private static final Logger logger =
            Logger.getLogger(GraphicalView.class.getName());
    GraphicalController gc;
    DefaultListModel listModel;
    protected JPanel elementsPanel;
    protected JPanel controlPanel;
    protected JPanel sheetPanel;
    protected JPanel statusPanel;
    protected JFrame mainFrame;
    protected JTextArea statusTextArea;
   
    protected JButton stepButton;
    
    private JScrollPane jScrollPane1;
    protected JList list;
    
    public GraphicalView(GraphicalController c) {
        gc = c;
        logger.info("Creating GUI from " + this);
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            
            public void run() {
                createAndShowGUI();
                
            }
        });
    }

    /**
     * Menü bárt csinálja!
     * @return
     */
    public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        // Megcsinájja a menü bárt.
        menuBar = new JMenuBar();

        // A file menü.
        menu = new JMenu("File");
        menuBar.add(menu);

   
        
        
        menuItem = new JMenuItem("Exit");
        menuItem.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                logger.info("Window closed from menu");
                System.exit(0);
            }
        });
        menu.add(menuItem);

        // return the menuBar
        return menuBar;
    }

    // A választható elemek listájának megkreálása
    private JList createElementSelectionList() {
        listModel = new DefaultListModel();
        
        listModel.addElement("AND Gate");
        listModel.addElement("OR Gate");
        listModel.addElement("Inverter");
        listModel.addElement("Composite");
        listModel.addElement("LED");
        listModel.addElement("Oscilloscope");
        listModel.addElement("Button");
        listModel.addElement("Rategen");
        listModel.addElement("Wire");
        listModel.addElement(" [X]Delete[X]");
        
        
        list = new JList(listModel);
        list.setSelectedIndex(8);
        
        return list;
    }
    
    private void createAndShowGUI() {
        //Create and set up the window.
        mainFrame = new JFrame("digIT");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        mainFrame.setJMenuBar(createMenuBar());
        
        
        elementsPanel = new javax.swing.JPanel();
        controlPanel = new javax.swing.JPanel();
        
        stepButton = new javax.swing.JButton();
       
        statusPanel = new javax.swing.JPanel();
        statusTextArea = new javax.swing.JTextArea();
        sheetPanel = new SheetPanel(gc);
        sheetPanel.setBackground(Color.WHITE);



        // GraphicalController add
        sheetPanel.addMouseListener(gc);
        
        jScrollPane1 = new javax.swing.JScrollPane();
        
        mainFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        
        elementsPanel.setBackground(new java.awt.Color(255, 255, 255));
        elementsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Elements"));
        
        jScrollPane1.setViewportView(createElementSelectionList());
        javax.swing.GroupLayout elementsPanelLayout = new javax.swing.GroupLayout(elementsPanel);
        elementsPanel.setLayout(elementsPanelLayout);
        elementsPanelLayout.setHorizontalGroup(
                elementsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE));
        
        elementsPanelLayout.setVerticalGroup(
                elementsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE));
        
        controlPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Control"));
        
       
        
        stepButton.setText("Step");
        stepButton.addActionListener(gc);
        
       
        
        
        javax.swing.GroupLayout controlPanelLayout = new javax.swing.GroupLayout(controlPanel);
        controlPanel.setLayout(controlPanelLayout);
        controlPanelLayout.setHorizontalGroup(
                controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(controlPanelLayout.createSequentialGroup().addContainerGap().addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(stepButton).addContainerGap(83, Short.MAX_VALUE)));
        controlPanelLayout.setVerticalGroup(
                controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(stepButton)));
        
        statusPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Status"));
        
        statusTextArea.setBackground(new java.awt.Color(0, 0, 0));
        statusTextArea.setEditable(false);
        statusTextArea.setForeground(new java.awt.Color(51, 255, 0));
        statusTextArea.setToolTipText("Status messages");
        statusTextArea.setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));
        
        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
                statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(statusTextArea, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE));
        statusPanelLayout.setVerticalGroup(
                statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(statusTextArea, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE));
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(mainFrame.getContentPane());
        mainFrame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(elementsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(controlPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(statusPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(sheetPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(controlPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(sheetPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(statusPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(elementsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        
        mainFrame.pack();
        //Display the window.
        mainFrame.setSize(640, 480);
        mainFrame.setMinimumSize(new Dimension(640, 480));
        mainFrame.setVisible(true);
    }
}
